from PyQt5.QtCore import Qt
import json

class ClassesLabel:
    def __init__(self, name: str, index: int):
        self.name = name
        self.index = index

    def __repr__(self):
        return f'{self.name}:{self.index}'

class PainterInfo:
    def __init__(self, color: Qt.GlobalColor, label_str: str):
        self.color = color
        self.label = label_str
    def __repr__(self):
        return f'{self.color}:{self.label}'


class RectInfo:
    def __init__(self, rect, label_int, painter_info: PainterInfo):
        self.rect = rect
        self.painter_info = painter_info
        self.label = label_int

    def to_yolo_format(self, image_size):
        x_center = (self.rect.left() + self.rect.right()) / 2 / image_size.width()
        y_center = (self.rect.top() + self.rect.bottom()) / 2 / image_size.height()
        width = self.rect.width() / image_size.width()
        height = self.rect.height() / image_size.height()
        return f'{self.label} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}'
    def __repr__(self):
        return f'{self.rect}:{self.painter_info}, {self.label}'


class DataStructsTools:
    @staticmethod
    def get_label_list_from_json(path):
        with open(path, 'r') as f:
            temp_dict = json.load(f)
        return [ClassesLabel(name, index) for name, index in temp_dict.items()]

    @staticmethod
    def get_label_dict_from_json(path):
        with open(path, 'r') as f:
            temp_dict = json.load(f)
        return [ClassesLabel(name, index) for name, index in temp_dict.items()]